<script setup>
import { ref } from 'vue'
import { Delete, Edit } from '@element-plus/icons-vue'
import ChannelSelect from '@/views/article/components/ChannelSelect.vue'
import { artGetListService } from '@/api/article.js'
import PageContainer from '@/components/PageContainer.vue'
import { formatTime } from '@/utils/dateFormat.js'
import ArticleEdit from '@/views/article/components/ArticleEdit.vue'

// 页面变量
const loading = ref(false)
const articleEditRef = ref()

const articleList = ref([])
const total = ref(0)
const params = ref({
  pagenum: 1,
  pagesize: 10,
  cate_id: '',
  state: ''
})
const getArticleList = async () => {
  loading.value = true
  console.log(params.value)
  const res = await artGetListService(params.value)
  articleList.value = res.data.data
  total.value = res.data.total
  loading.value = false
}
// 进入页面调用
getArticleList()

/**
 * 添加文章
 */
const addArticle = () => {
  articleEditRef.value.open({})
}

/**
 * 编辑文章
 * @param row 选中的文章数据
 */
const onEditArticle = (row) => {
  articleEditRef.value.open(row)
}

const onDelete = (row) => {
  console.log(row)
}

/**
 * 修改页面大小
 * @param size 分页页面大小
 */
const handleSizeChange = (size) => {
  // 页码变化，刷新页面，渲染第一页
  params.value.pagenum = 1
  params.value.pagesize = size
  getArticleList()
}

/**
 * 改变页码
 * @param page 最新的页码
 */
const handleCurrentChange = (page) => {
  params.value.pagenum = page
  getArticleList()
}

/**
 * 搜索内容：搜索时，从第一页开始渲染
 */
const search = () => {
  params.value.pagenum = 1
  getArticleList()
}

/**
 * 重置
 */
const reset = () => {
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  getArticleList()
}

/**
 * 添加、编辑成功时
 */
const onSuccess = () => {
  getArticleList()
}
</script>
<template>
  <page-container title="文章管理">
    <template #extra>
      <el-button @click="addArticle">添加文章</el-button>
    </template>

    <!-- 表单区域 -->
    <el-form inline>
      <el-form-item label="文章分类：">
        <channel-select v-model="params.cate_id"></channel-select>
      </el-form-item>
      <el-form-item label="发布状态：">
        <el-select v-model="params.state" style="width: 200px">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">搜索</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格区域 -->
    <el-table :data="articleList" v-loading="loading">
      <el-table-column label="文章标题" prop="title">
        <template #default="{ row }">
          <el-link type="primary" :underline="false"> {{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="分类" prop="cate_name"></el-table-column>
      <el-table-column label="发表时间" prop="pub_date">
        <template #default="{ row }">
          {{ formatTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="state"></el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button circle plain type="primary" :icon="Edit" @click="onEditArticle(row)">
          </el-button>
          <el-button circle plain type="danger" :icon="Delete" @click="onDelete(row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-model:current-page="params.pagenum"
      v-model:page-size="params.pagesize"
      :page-sizes="[10, 20, 50, 100]"
      :size="'default'"
      :disabled="false"
      :background="true"
      layout="jumper, total, sizes, prev, pager, next"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />
    <article-edit ref="articleEditRef" @success="onSuccess"></article-edit>
  </page-container>
</template>
